package com.zoght.financial.model;

public class StockInfoBean {
	
	private String symbol=null, description=null;
	private int transactions;
	private int boughtShares, soldShares, stillHolding;
	private double boughtSharesCost, soldSharesProceeds;
	private double averageShareBoughtCost, averageShareSoldCost;
	private double realizedGainsLosses, currentCost;
	
	public String getSymbol() {
		return symbol;
	}
	public void setSymbol(String symbol) {
		this.symbol = symbol;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
	public void inc(TransactionBean.Type type, int shares, double pricePerShare, double total) {
		if (type == TransactionBean.Type.Buy) {
			boughtShares += shares;
			boughtSharesCost += (-1)*total;
		} else {
			soldShares += (-1) * shares;
			soldSharesProceeds += total;			
		}
		transactions++;
	}
	
	public void close() {
		averageShareBoughtCost = boughtSharesCost/boughtShares;
		if (soldShares != 0) {
			averageShareSoldCost   = soldSharesProceeds/soldShares;
		}
		int diff = boughtShares - soldShares;
		if (diff == 0) {
			realizedGainsLosses = soldSharesProceeds - boughtSharesCost;
			stillHolding = 0;
		}
		else {
			stillHolding = diff;
			if (soldShares != 0 ) {
				realizedGainsLosses = (averageShareSoldCost-averageShareBoughtCost)*soldShares;
			} else {
				realizedGainsLosses = 0;
			}
			currentCost = stillHolding*(averageShareBoughtCost);
		}
	}
	public int getTransactions() {
		return transactions;
	}
	public int getBoughtShares() {
		return boughtShares;
	}
	public int getSoldShares() {
		return soldShares;
	}
	public int getStillHolding() {
		return stillHolding;
	}
	public double getBoughtSharesCost() {
		return boughtSharesCost;
	}
	public double getSoldSharesProceeds() {
		return soldSharesProceeds;
	}
	public double getAverageShareBoughtCost() {
		return averageShareBoughtCost;
	}
	public double getAverageShareSoldCost() {
		return averageShareSoldCost;
	}
	public double getRealizedGainsLosses() {
		return realizedGainsLosses;
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("[" + getSymbol() + " , ");
		sb.append("transactions= " + transactions + " , ");
		sb.append("Number of shares holding= " + getStillHolding() + " , ");
		sb.append("Realized Gains/Loss= " + getRealizedGainsLosses() + " , ");
		sb.append("Current Cost= " + getCurrentCost() + " ] ");
		return sb.toString();
	}
	public double getCurrentCost() {
		return currentCost;
	}
	
	
}
